package com.lovo.frame;



import com.lovo.pojo.UserDO;
import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.GetConnectionUtil;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Modifyjpanel extends JPanel implements ActionListener {
    private Icon sureimage = Util.getIcon("/images/buttonSure.jpg");
    private Icon offimage = Util.getIcon("/images/buttonOff.jpg");
    private JButton SurejButton;
    private JButton OffjButton;
    private Mainjframe frame;
    private JTextField textField1;
    private JTextField textField2;
    private JLabel jLabel;
    private JLabel jLabel1;
    private JTextField textField0;
    private JLabel jLabel0;
    private UserService userService = new UserServiceImpl();

    public Modifyjpanel(Mainjframe frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        jLabel0 = new JLabel("请输入旧密码：");
        jLabel0.setForeground(Color.BLACK);
        jLabel0.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel0.setBounds(340, 160, 200, 40);
        this.add(jLabel0);

        textField0 = new JTextField();
        textField0.setBounds(550, 160, 350, 40);
        this.add(textField0);

        jLabel = new JLabel("请输入新密码：");
        jLabel.setForeground(Color.BLACK);
        jLabel.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel.setBounds(340, 220, 200, 40);
        this.add(jLabel);

        textField1 = new JTextField();
        textField1.setBounds(550, 220, 350, 40);
        this.add(textField1);

        jLabel1 = new JLabel("请再次输入新密码：");
        jLabel1.setForeground(Color.BLACK);
        jLabel1.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel1.setBounds(340, 280, 200, 40);
        this.add(jLabel1);

        textField2 = new JTextField();
        textField2.setBounds(550, 280, 350, 40);
        this.add(textField2);

        SurejButton = new JButton(sureimage);
        SurejButton.setBounds(360, 500, 140, 50);
        this.add(SurejButton);
        SurejButton.addActionListener(this);
        OffjButton = new JButton(offimage);
        OffjButton.setBounds(700, 500, 140, 50);
        this.add(OffjButton);
        OffjButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == SurejButton) {
            int id = frame.getLoginUser().getId();
            String oldpwd = textField0.getText();
            String newpwd = textField1.getText();
            String newpwd1 = textField2.getText();
            String result = userService.ModifyMoney(id, oldpwd, newpwd, newpwd1);
            JOptionPane.showMessageDialog(this,result);
            textField0.setText("");
            textField1.setText("");
            textField2.setText("");
            if (result.equals("修改密码成功，请重新登录")) {
                frame.switchPanel("login");
            }
        } else if (e.getSource() == OffjButton) {
            frame.switchPanel("menu");
        }
    }
}
